In [1]:
import sim
import klib
import plotly.express as px

策略结果展示¶

载入klib module 和 sim 模块可以快速进行策略结果分析。

In [2]:
pwd
Out[2]:
'/home/ubuntu/work/klib'
In [3]:
# 输入Backtest或实盘log路径
backtest_log_path = "sample_log/run_backtest/build_pos-20220114-20220517-20295" 
s = sim.Sim(backtest_log_path)
df_summary = s.read_summary()
sample_log/run_backtest/build_pos-20220114-20220517-20295/backtest/account_settlement-20220114-20220517.csv
log file found.

查看交易记录¶

In [4]:
s.read_trades("20220106", "20220517") # 给定start_data, end_date
Out[4]:
exchange order_ref trade_id instrument_id strategy_id account_id direction offset price qty
timestamp
2022-01-14 21:01:17.012 DCE 202201171234 123450101 i2209 1234 12345678 SELL OPEN 688.0 2
2022-01-17 14:55:00.563 DCE 202201171235 123460101 i2209 1234 12345678 BUY CLOSE_TODAY 677.0 2
2022-01-17 21:01:00.913 DCE 202201181234 124470101 i2209 1234 12345678 BUY OPEN 680.0 4
2022-01-18 14:55:01.475 DCE 202201181235 124480101 i2209 1234 12345678 SELL CLOSE_TODAY 685.5 4
2022-01-18 21:01:00.521 DCE 202201191234 125490101 i2209 1234 12345678 BUY OPEN 692.0 1
... ... ... ... ... ... ... ... ... ... ...
2022-05-13 14:55:39.860 DCE 202205131235 127590101 i2209 1234 12345678 SELL CLOSE_TODAY 824.0 100
2022-05-13 21:01:00.456 DCE 202205161234 128600101 i2209 1234 12345678 BUY OPEN 822.5 25
2022-05-16 14:55:00.440 DCE 202205161235 128610101 i2209 1234 12345678 SELL CLOSE_TODAY 827.5 25
2022-05-16 21:01:00.680 DCE 202205171234 129620101 i2209 1234 12345678 SELL OPEN 839.5 17
2022-05-17 14:55:00.051 DCE 202205171235 129630101 i2209 1234 12345678 BUY CLOSE_TODAY 832.0 17

134 rows × 10 columns

每日净收益(除去手续费)¶

In [5]:
px.bar(df_summary, df_summary.index, y = "net_pnl", title = "daily netpnl")

策略净值¶

In [6]:
pure_value = df_summary.balance/df_summary.pre_balance[0]
px.line(pure_value, title = "净值")

策略统计数据 Performance Metrics¶

In [7]:
brief_summary = klib.get_brief_summary(df_summary)
display(brief_summary)
交易日 开始日 结束日 起始资金 结束资金 净值 胜率 盈亏比 总盈利 手续费 夏普 回撤
0 88 2022-01-14 2022-05-17 2000000.0 2986897.415 1.493449 0.454545 2.182277 986897.415 21952.585 2.86832 -121070.145